#include <stdio.h>
#include <stdlib.h>
#include "Terrain.h"


void placer_du_bois(Terrain * terrain)
{
    int i;
    int alea;
    srand(time(NULL));

    for(i=128;i<672;i++)
    {
        alea=rand()%5;
        if(alea==0 && terrain->collision[i]==0)
        {
            terrain->objet[i]=1;
            terrain->collision[i]=2;
        }
    }
}



int * charger_collision(char * nom_fic)
{
    int i;
    int * tab=(int*)malloc(672*sizeof(int));

    FILE * fic = fopen(nom_fic,"r");
    if(fic==NULL)
    {
        printf("erreur\n");
    }

    else
    {
        for(i=0;i<672;i++)
        {
            if(i==671)
            {
                fscanf(fic, "%d", &(tab[i]));
            }

            else
            {
                fscanf(fic, "%d\n", &(tab[i]));
            }
        }
    }

    fclose(fic);
    return tab;
}


Terrain * initialiser_terrain()
{
    Terrain * terrain=(Terrain*)malloc(sizeof(Terrain));
    terrain->dimx=32;
    terrain->dimy=21;

    terrain->decor=(int*)malloc(672*sizeof(int));
    terrain->objet=(int*)malloc(672*sizeof(int));

    terrain->collision_zone0=charger_collision("./src/data/collisions/zone0.txt");
    terrain->collision_zone1=charger_collision("./src/data/collisions/zone1.txt");
    terrain->collision_zone2=charger_collision("./src/data/collisions/zone2.txt");
    terrain->collision_zone3=charger_collision("./src/data/collisions/zone3.txt");
    terrain->collision_zone4=charger_collision("./src/data/collisions/zone4.txt");
    terrain->collision_zone5=charger_collision("./src/data/collisions/zone5.txt");

    terrain->collision=terrain->collision_zone0;

    /* On cree les bouts de bois au hasard sur la carte */
    //placer_du_bois(terrain);

    return terrain;
}
